package com.alstudio.utils.distributor.distributor;

import com.alstudio.net.protocol.xmpp.data.ServerInfo;
import com.alstudio.ui.base.BaseActivity;
import com.alstudio.utils.ALActivityManager;
import com.alstudio.utils.distributor.versioncheck.VersionCheckUtils;
import com.alstudio.utils.log.Mylog;
import com.alstudio.view.toast.MyToastUtil;

public class DistributorManager implements DistributorEventListener {

	private static DistributorManager instance;

	public DistributorManager() {
		// TODO Auto-generated constructor stub
		VersionCheckUtils.getInstance().setDistributorEventListener(this);
	}

	public static DistributorManager getInstance() {
		if (instance == null) {
			instance = new DistributorManager();
		}
		return instance;
	}

	public void checkDistributor(String currentVersion) {

		final String msg = "<query platform=\"Android\" version=\""
				+ currentVersion + "\"/>";
		Mylog.d("write to server by socket connection: " + msg);

		VersionCheckUtils.getInstance().checkVersion("116.254.203.70", "9225",
				msg, false, false);
	}

	public void checkDistributor(String currentVersion, boolean bySocket) {
		final String msg = "<query platform=\"Android\" version=\""
				+ currentVersion + "\"/>";
		Mylog.d("write to server by socket connection: " + msg);
		VersionCheckUtils.getInstance().checkVersion("116.254.203.70", "9225",
				msg, bySocket, false);
	}

	@Override
	public void onConnectSuccess() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onConnectFail(String reason) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onCheckDistributorSuccess(ServerInfo info) {
		// TODO Auto-generated method stub

		if (info != null) {

			BaseActivity a = ALActivityManager.getALActivityManager()
					.getCurrentActivity();
			if (a == null) {
				return;
			}
			// MyToastUtil.showToastOnTop(a,
			// "阈炬帴鍒嗗彂鍣ㄦ垚锷熶简 " + info.getIMServerIP(), 4000);

		}
	}

	@Override
	public void onCheckDistributorFailed(String reason) {
		// TODO Auto-generated method stub
		BaseActivity a = ALActivityManager.getALActivityManager()
				.getCurrentActivity();
		if (a == null) {
			return;
		}
		MyToastUtil.showToastOnTop(a, "阈炬帴鍒嗗彂鍣ㄥけ璐?" + reason, 4000);
	}
}
